Skip to content

✨ feat(launchd): resident HTTP service lifecycle (PR2 — launchd) - #14

Closed
SWSAmor wants to merge 2 commits into
feat/single-instance-http-serverfrom
feat/launchd-lifecycle
Closed

SWSAmor wants to merge 2 commits into
feat/single-instance-http-serverfrom
feat/launchd-lifecycle

Conversation

@SWSAmor

@SWSAmor SWSAmor commented Jul 24, 2026

Copy link
Copy Markdown
Owner

PR2 — launchd lifecycle

Stacked on #13 (PR1 — HTTP core). Base is feat/single-instance-http-server; GitHub will auto-retarget to main when #13 merges. Reviewing after #13 shows only this PR's diff.

Makes the single shared HTTP instance an always-resident, launchd-managed macOS service, and lets an idle resident stay lightweight.

1. Lazy/idle downstream pool (✨ feat(core))

Gated by CODE_EXECUTOR_POOL_IDLE_MS (0 = disabled = eager, the default; stdio behavior byte-for-byte unchanged).

  • Lazy build — the downstream MCP pool is built on the first tool call (SharedCore.ensurePoolReady, idempotent for concurrent callers), not eagerly at startup.
  • Idle teardown — when the last HTTP session closes, an idle timer disconnects the pool after the window (reclaiming the ~15 downstream children); the next call rebuilds it. Deferred while an execution is in flight; cancelled on reconnect or full shutdown.
  • Session accounting driven from the Streamable HTTP transport (onSessionOpen/Close), counted once per session.
  • SharedCore gains optional constructor injection (pool + connection pool) for testability; production call sites unchanged.

2. Resident launchd service + service CLI (✨ feat(service))

  • LaunchdService — renders a resident-daemon plist (RunAtLoad + KeepAlive, ProgramArguments=[binary], EnvironmentVariables = http role + port + bearer token + MCP_CONFIG_PATH + CODE_EXECUTOR_ALLOW_NESTED) and manages it via modern launchctl verbs (bootstrap/bootout/kickstart/print). Distinct from the StartCalendarInterval sync scheduler. Plist written 0600 (embeds the token); all interpolated values XML-escaped; label charset-validated (no path traversal); launchctl runner + platform injectable for tests.
  • ensureHttpToken — read-or-generate a 256-bit bearer token persisted 0600 at ~/.code-executor/http-token (idempotent).
  • code-executor-mcp service install|uninstall|status|restart — argv-dispatched like setup/sync-wrappers; --port/--config/--binary/--no-auth; prints a host connection hint.

Verification

  • typecheck clean; 30 new unit tests green (launchd-service, http-token, idle-pool with fake timers).
  • Binary integration tests 7/7 on a fresh binary (session accounting confirmed in the real binary).
  • Full suite (sandbox-off, matching the established exclusions): only the known filesystem.test.ts env-failure — no new regressions.
  • Live resident smoke (empty config, port 39274, --no-auth, self-cleaning): install → launchd loads it → GET /health 200 {"healthy":true,...} → uninstall removes it cleanly.

Deferred (not this PR)

Homebrew formula / brew services, configure-hosts, token rotation, full cectl (PR3/PR4). Per-session audit/rate-limit keys already landed in PR1 (#13).

🤖 Generated with Claude Code

SWSAmor and others added 2 commits July 24, 2026 16:33
The single resident HTTP instance can now stay lightweight when idle.
Gated by CODE_EXECUTOR_POOL_IDLE_MS (0 = disabled = eager, the default and
the unchanged stdio behavior):

- Lazy build: the downstream MCP pool is initialized on the FIRST tool call
  (SharedCore.ensurePoolReady, idempotent for concurrent callers) instead of
  eagerly at startup.
- Idle teardown: when the last HTTP session closes, an idle timer tears the
  pool down (MCPClientPool.disconnect) after the configured window, reclaiming
  the ~15 downstream children; the next tool call rebuilds it. Teardown is
  deferred while an execution is in flight and cancelled on reconnect or full
  shutdown.
- Session accounting is driven from the Streamable HTTP transport
  (onSessionOpen/onSessionClose), gated once per session on the sessions map.

SharedCore gains optional constructor injection (pool + connection pool) for
testability; production call sites are unchanged.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Add the macOS launchd lifecycle for the single shared HTTP instance so it can
run as an always-resident, managed service (a host cannot spawn an HTTP MCP
server the way a stdio host does, so the server must already be running).

- LaunchdService: renders a resident-daemon plist (RunAtLoad + KeepAlive,
  ProgramArguments=[binary], EnvironmentVariables carrying the http role, port,
  bearer token, MCP_CONFIG_PATH and CODE_EXECUTOR_ALLOW_NESTED) and manages it
  via modern launchctl verbs (bootstrap/bootout/kickstart/print). Distinct from
  the StartCalendarInterval sync scheduler. Plist written 0600 (it embeds the
  token); all interpolated values XML-escaped; label charset-validated so it
  cannot traverse out of ~/Library/LaunchAgents; launchctl runner + platform
  injectable for tests.
- ensureHttpToken: read-or-generate a 256-bit bearer token persisted 0600 at
  ~/.code-executor/http-token (idempotent).
- `code-executor-mcp service install|uninstall|status|restart` CLI (argv-
  dispatched like setup/sync-wrappers), with --port/--config/--binary/--no-auth
  and a connection hint for wiring a host.

Verified live: install → launchd loads it → GET /health 200 → uninstall
removes it cleanly.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@SWSAmor
SWSAmor deleted the branch feat/single-instance-http-server July 24, 2026 15:39
@SWSAmor SWSAmor closed this Jul 24, 2026
@SWSAmor
SWSAmor deleted the feat/launchd-lifecycle branch July 24, 2026 15:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant